<template>
	<view>
		<view class="uni-box">
			<uni-forms label-width="120px" label-align="center" ref="formRole" :modelValue="formData.bodys[0]">
				<uni-forms-item label="供应商:" required name="supplierId">
					<uni-data-select v-model="formData.header.supplierId"
						:localdata="supplierSelector"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="药品名称:" required :rules="[{ required: true, errorMessage: '请输入药品名称' }]"
					name="name">
					<uni-easyinput v-model="formData.bodys[0].name" placeholder="请输入内容"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item label="规格:" required :rules="[{ required: true, errorMessage: '请输入规格' }]"
					name="specification">
					<view class="_itemVals">
						{{formData.bodys[0].specification}}
					</view>
				</uni-forms-item>
				<!-- 	<uni-forms-item label="药品类型:" required :rules="[{ required: true, errorMessage: '请输入药品类型' }]"
					name="category">
					<uni-easyinput v-model="formData.category" placeholder="请输入内容"></uni-easyinput>
				</uni-forms-item> -->
				<uni-forms-item label="批号:" required :rules="[{ required: true, errorMessage: '请输入批号' }]"
					name="batchNumber">
					<view class="_itemVals">
						<uni-easyinput v-model="formData.bodys[0].batchNumber" placeholder="请输入批号"></uni-easyinput>
					</view>
				</uni-forms-item>
				<uni-forms-item label="进货数量:" required :rules="[{ required: true, errorMessage: '请输入进货数量' }]"
					name="quantity">
					<view class="_itemVals">
						<uni-easyinput v-model="formData.bodys[0].quantity" placeholder="请输入数量"></uni-easyinput>
					</view>
				</uni-forms-item>
				<!-- 	<uni-forms-item label="转换零售单位:" required :rules="[{ required: true, errorMessage: '请输入转换零售单位' }]"
					name="supplierName">
					<view class="_itemVals">
						1
					</view>
				</uni-forms-item> -->
				<uni-forms-item label="有限期:" required :rules="[{ required: true, errorMessage: '请输入有限期' }]"
					name="endDate">
					<view class="_itemVals">
						<uni-datetime-picker type="date" :clear-icon="false" v-model="formData.bodys[0].endDate" />
					</view>
				</uni-forms-item>
				<uni-forms-item label="进货单价(元):" required :rules="[{ required: true, errorMessage: '请输入进货单价' }]"
					name="purchasePrice">
					<view class="_itemVals">
						<uni-easyinput v-model="formData.bodys[0].purchasePrice" placeholder="请输入进货单价"></uni-easyinput>
					</view>
				</uni-forms-item>
				<uni-forms-item label="零售价(元):" required :rules="[{ required: true, errorMessage: '请输入零售价' }]"
					name="retailPrice">
					<view class="_itemVals">
						<uni-easyinput v-model="formData.bodys[0].retailPrice" placeholder="请输入零售价"></uni-easyinput>
					</view>
				</uni-forms-item>
			</uni-forms>
		</view>

		<view class="footer">
			<button @click="clone">
				取消
			</button>
			<button type="primary" @click="onPharmacyAdd">
				入库
			</button>
		</view>
	</view>
</template>

<script setup>
	import {
		onLoad
	} from '@dcloudio/uni-app';
	import {
		getDetail,
		getAdds
	} from "./request.js"
	import {
		reactive,
		ref
	} from "vue";


	import getOptions from "@/utils/options/getSelectList.js"

	let supplierSelector = ref([])
	getOptions('supplierSelector', supplierSelector).then((res) => {
		supplierSelector.value = res
	})


	const formRole = ref(null)
	const formData = ref({
		bodys: [{}],
		header: {},
	})

	const clone = () => {
		uni.redirectTo({
			url: '/pages/view/purchase/index'
		})
	}

	const onPharmacyAdd = async () => {
		formRole.value.validate().then(async ({
			valid,
			errors
		}) => {
			if (valid) {
				console.log('失败');
			} else {
				getAdds(formData.value).then((res) => {
					if (res.msg == 200) {
						uni.showToast({
							title: '入库成功',
							icon: 'success',
							 duration: 3000,
							success() {
								uni.redirectTo({
									url: '/pages/view/purchase/index'
								})
							}
						});
					} else {
						uni.showToast({
							title: res.msg,
							duration: 2000,
							icon: 'error',
						});
					}
				})
			}
		});
	};



	onLoad((e) => {
		let detailId = e.id
		getDetail({
			id: detailId
		}).then((res) => {
			formData.value.bodys[0] = res.data.bodys[0]
			formData.value.header = res.data.head
		})
	})
</script>

<style lang="scss">
	._itemVals {
		display: flex;
		align-items: center;
		height: 100%;
	}

	.uni-box {
		padding: 18rpx 0;
	}

	.uni-forms-item {
		border-bottom: 1rpx solid #ccc;
		margin-bottom: 12rpx !important;
		padding-bottom: 12rpx;
	}

	.footer {
		position: fixed;
		bottom: 10rpx;
		left: 0;
		right: 0;
		padding: 6rpx;
		display: flex;
		justify-content: space-between;

		button {
			margin: 0 6px;
			width: 50%;
		}
	}
</style>